package zd.cornermemory.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Statistics {

	/**
	 * 根据打乱公式字符串截取
	 * @param str
	 * @return
	 */
	static List<String> getCodes(String str){
		String regex = "([URFDLBEMSxyz])(w?)(['2]?)";
		str = str.replaceAll("\\s","");
		List<String> list = new ArrayList<>();
		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(str);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}

	public static String getFormatCodes(String codes){
		List<String> list = getCodes(codes);
		String code = "";
		for (String c : list) {
			code += c+" ";
		}
		code = code.trim();
		return code;
	}

	static String timeToString(int hour, int min, int sec, int msec) {
		StringBuilder time = new StringBuilder();
		if(hour == 0) {
			if(min==0) time.append(""+sec);
			else {
				if(sec<10)time.append(""+min+":0"+sec);
				else time.append(""+min+":"+sec);
			}
		}
		else {
			time.append(""+hour);
			if(min<10)time.append(":0"+min);
			else time.append(":"+min);
			if(sec<10)time.append(":0"+sec);
			else time.append(":"+sec);
		}

		if(msec<10)time.append(".00"+msec);
		else if(msec<100)time.append(".0"+msec);
		else time.append("."+msec);

		return time.toString();
	}
	
	public static String timeToString(int t) {
		boolean m = t < 0;
		if(m) t = -t;
		int msec = t % 1000;
		int sec = t / 1000, min = 0, hour = 0;
		min = sec / 60;
		sec %= 60;
		hour = min / 60;
		min %= 60;
		return (m ? "-" : "") + timeToString(hour, min, sec, msec);
	}

	public static String handleLongDate(Long date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date da = new Date(date);
		return format.format(da);
	}
	public static String handleLongDate(Long date,int detail) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date da = new Date(date);
		return format.format(da);
	}
}
